package org.jeecg.modules.controller;

import java.io.*;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.net.URLDecoder;
import javax.activation.MimetypesFileTypeMap;
import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.jeecg.common.api.ding.DingApi;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.entity.DiyProfessorInfo;
import org.jeecg.modules.entity.DiyProfessorScore;
import org.jeecg.modules.entity.DiyTableStatus;
import org.jeecg.modules.service.DiyProfessorScoreService;
import org.jeecg.modules.service.IDiyProfessorInfoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.service.IDiyTableStatusService;
import org.jeecg.modules.util.ProfessorStatus;
import org.jeecg.modules.vo.ExportProfessorInfo;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 专家信息录入
 * @Author: Zhang Haoyu
 * @Date: 2020-05-21
 * @Version: V1.0
 */
@Api(tags = "专家信息录入")
@RestController
@RequestMapping("/org/diyProfessorInfo")
@Slf4j
public class DiyProfessorInfoController extends JeecgController<DiyProfessorInfo, IDiyProfessorInfoService> {
    @Autowired
    private IDiyProfessorInfoService diyProfessorInfoService;
    @Autowired
    private DingApi dingApi;
    @Autowired
    private DiyProfessorScoreService diyProfessorScoreService;


    /**
     * 分页列表查询
     *
     * @param diyProfessorInfo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "专家信息录入-分页列表查询")
    @ApiOperation(value = "专家信息录入-分页列表查询", notes = "专家信息录入-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(DiyProfessorInfo diyProfessorInfo,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        Page<DiyProfessorInfo> page = new Page<>(pageNo, pageSize);
        IPage<DiyProfessorInfo> pageList = diyProfessorInfoService.pageByEntity(page, diyProfessorInfo);
        return Result.ok(pageList);
    }

    /**
     *   添加
     *
     * @param diyProfessorInfo
     * @return
     */
/*	@AutoLog(value = "专家信息录入-添加")
	@ApiOperation(value="专家信息录入-添加", notes="专家信息录入-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody DiyProfessorInfo diyProfessorInfo) {
		diyProfessorInfo.setStatus(ProfessorStatus.UNPROCESSED.getStatus());
		diyProfessorInfoService.save(diyProfessorInfo);
		return Result.ok("添加成功！");
	}*/

    /**
     * @param diyProfessorInfo
     * @return
     */
    @AutoLog(value = "专家信息-提交")
    @ApiOperation(value = "专家信息录入-提交", notes = "专家信息录入-提交")
    @PostMapping(value = "/add")
    public Result<?> sendToCheck(@RequestBody DiyProfessorInfo diyProfessorInfo) throws ApiException {
        diyProfessorInfo.setStatus(ProfessorStatus.UNPROCESSED.getStatus());

        // 获取其中的所有数据，放到钉钉表单中

        try {
//			 diyProfessorInfoService.sendToDingTalk(diyProfessorInfo);
            return Result.ok("已发送至审核！");
        } catch (Exception e) {
            return Result.error("出错了！请重新发送");
        }
/*
		 // 保存至专家信息表
		 diyProfessorInfoService.save(diyProfessorInfo);
		 // 添加至信息审核状态表中
		 DiyTableStatus diyTableStatus = new DiyTableStatus();
		 // 获取保存至专家信息表中的id放入状态表
		 diyTableStatus.setProfessorTableId(diyProfessorInfo.getId());
		 diyTableStatus.setCreateBy(diyProfessorInfo.getCreateBy());
		 diyTableStatus.setCreateTime(diyProfessorInfo.getCreateTime());
		 diyTableStatus.setProfessorTableId(diyTableStatus.getProfessorTableId());
		 diyTableStatus.setTableStatus(diyProfessorInfo.getStatus());
		 diyTableStatus.setUpdateTime(diyProfessorInfo.getUpdateTime());
		 diyTableStatus.setUpdateBy(diyProfessorInfo.getUpdateBy());
		 diyTableStatusService.save(diyTableStatus);
		 System.out.println(diyTableStatus);*/
    }


    /**
     * 编辑
     *
     * @param diyProfessorInfo
     * @return
     */
    @AutoLog(value = "专家信息录入-编辑")
    @ApiOperation(value = "专家信息录入-编辑", notes = "专家信息录入-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody DiyProfessorInfo diyProfessorInfo) {
        diyProfessorInfoService.updateById(diyProfessorInfo);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "专家信息录入-通过id删除")
    @ApiOperation(value = "专家信息录入-通过id删除", notes = "专家信息录入-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        diyProfessorInfoService.removeById(id);
        diyProfessorScoreService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "专家信息录入-批量删除")
    @ApiOperation(value = "专家信息录入-批量删除", notes = "专家信息录入-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.diyProfessorInfoService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }
    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "专家信息录入-通过id查询")
    @ApiOperation(value = "专家信息录入-通过id查询", notes = "专家信息录入-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        DiyProfessorInfo diyProfessorInfo = diyProfessorInfoService.getById(id);
        if (diyProfessorInfo == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(diyProfessorInfo);
    }
    /**
     * 导出excel
     *
     * @param request
     * @param diyProfessorInfo
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DiyProfessorInfo diyProfessorInfo) {
        return super.exportXls(request, diyProfessorInfo, DiyProfessorInfo.class, "专家信息录入");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DiyProfessorInfo.class);
    }




    /**
     * 根据ding_user_id 查询专家信息
     *
     * @return 专家信息
     */
    @GetMapping("/queryByDingUserId")
    public Result<?> queryByDingUserId(HttpServletRequest request) {
        //获取token
        String token = request.getHeader("X-Access-Token");
        //获取当前登录用户的unionid username就是unionid
        String username = JwtUtil.getUsername(token);
        // 根据unionid获取userid
        String useridStr = dingApi.selUserIdByUnionid(username);
        JSONObject getUserId = JSON.parseObject(useridStr);
        // 获取userId
        String userid = getUserId.getString("userid");

        DiyProfessorInfo info = diyProfessorInfoService.queryProfessorInfoByUserId(userid);
        if (info == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(info);
    }
}
